package com.dingding.special.user.account.biz.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.special.Specialaccounttradedetail;
import com.dingding.common.core.beans.special.Specialuseraccount;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.common.core.util.paravalidate.ValiResult;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.special.user.account.biz.ISpecialUserAccountService;
import com.dingding.special.user.account.code.UserAccountCodeMsg;
import com.dingding.special.user.account.dao.ISpecialAccountTradeDetailDao;
import com.dingding.special.user.account.dao.ISpecialUserAccountDao;
import com.dingding.special.user.account.dto.ReqSpecialAccountTradeDetailDTO;
import com.dingding.special.user.account.dto.ReqUserPacketDTO;
import com.dingding.special.user.account.dto.ResSpecialAccountTradeDetailDTO;
import com.dingding.special.user.account.dto.ResUserPacketDTO;
import com.dingding.special.user.account.dto.SpecialAccountTradeDetailDTO;
import com.dingding.special.user.deposit.code.DepositCodeMsg;
import com.dingding.special.user.deposit.dao.IUserDepositDao;

/**
 * 
 * @ClassName: ISpecialUserAccountService 
 * @Description: 专车用户账号Service实现类
 * @author YHQ
 * @date 2016年8月19日 下午3:35:21
 * @since V2.4.0
 * 
 */
@Component("specialUserAccountService")
public class SpecialUserAccountServiceImpl implements ISpecialUserAccountService {

	private Logger log = Logger.getLogger(this.getClass());
	
	@Resource(name="specialUserAccountDao")
	private ISpecialUserAccountDao specialUserAccountDao;
	
	@Resource(name="specialAccountTradeDetailDao")
	private ISpecialAccountTradeDetailDao specialAccountTradeDetailDao;
	
	@Resource(name="userDepositDao")
	private IUserDepositDao userDepositDao;
	
	/**
	 * 
	 * @Title: getUserPacket 
	 * @Description: 获取用户钱包 
	 * @param reqUserPacketDTO 获取用户钱包请求Bean
	 * @author YHQ 
	 * @return ResponseEntity<ResUserPacketDTO>    返回类型
	 * 
	 */
	@Override
	public ResponseEntity<ResUserPacketDTO> getUserPacket(ReqUserPacketDTO reqUserPacketDTO) {
		
		// 获取用户钱包响应Bean
		ResponseEntity<ResUserPacketDTO> response = new ResponseEntity<ResUserPacketDTO>();
		
		boolean flag = this.validateParam(response, reqUserPacketDTO);
		
		if(!flag){
			return response;
		}
		
		// 获取用户钱包信息
		ResUserPacketDTO resUserPacketDTO = specialUserAccountDao.gePacketInfo(reqUserPacketDTO.getUserId());
		
		if(resUserPacketDTO == null){
			
			response.setCode(HttpStatus.SC_FORBIDDEN);
			response.setErrorMessage(UserAccountCodeMsg.USER_IS_NOT_EXIST);// 用户不存在
			response.setPromptMessage(UserAccountCodeMsg.USER_IS_NOT_EXIST);// 用户不存在
			return response;
		}
		
		response.setCode(HttpStatus.SC_OK);
		response.setResponseBody(resUserPacketDTO);
		
		return response;
	}
	
	/**
	 * 
	 * @Title: validateParam 
	 * @Description: 获取用户钱包验证请求参数
	 * @param reqUserPacketDTO 获取用户钱包请求参数
	 * @author YHQ 
	 * @return boolean    返回类型
	 * 
	 */
	public boolean validateParam(ResponseEntity<ResUserPacketDTO> response,ReqUserPacketDTO reqUserPacketDTO){
		
		// 判断客户端请求数据是否为空
		ValiResult valiResult = ValidateUtil.validateBeanIsAvaliable(reqUserPacketDTO, ReqUserPacketDTO.class);
		
		if (!valiResult.isAvailable()) {
			response.setCode(HttpStatus.SC_FORBIDDEN);
			response.setErrorMessage(DepositCodeMsg.DATA_NULL_PARAM_MESSAGE);// 客户端请求数据为空
			response.setPromptMessage(DepositCodeMsg.APP_ERROR);// 客户端请求数据为空
			response.setNullProperties(valiResult.getNullFieldName());
			log.error("为空字段为：" + valiResult.getNullFieldName());
			return false;
		}
		
		return true;
	}

	
	/**
	 * 
	 * @Title: getUserAccountTradeDetail 
	 * @Description:  获取用户收支明细
	 * @param reqSpecialAccountTradeDetailDTO 获取用户收支明细请求Bean
	 * @author YHQ 
	 * @since V2.4.0
	 * @return ResponseEntity<ResSpecialAccountTradeDetailDTO>    返回类型
	 * 
	 */
	@Override
	public ResponseEntity<ResSpecialAccountTradeDetailDTO> getUserAccountTradeDetail(ReqSpecialAccountTradeDetailDTO tradeDetailDTO){
		
		ResponseEntity<ResSpecialAccountTradeDetailDTO> response = new ResponseEntity<ResSpecialAccountTradeDetailDTO>();
		
		// 验证请求参数是否为空
		boolean flag = this.validateTradeDetail(response, tradeDetailDTO);
		
		if(!flag){
			return response;
		}
		
		// 获取用户收支明细
		this.incomeAndExpensesList(response, tradeDetailDTO);
		
		// 总支出、总收入和总体现
		this.cumulativeDetail(response, tradeDetailDTO.getUserId());
		
		response.setCode(HttpStatus.SC_OK);
		
		return response;
	}
	
	/**
	 * 
	 * @Title: validateParam 
	 * @Description: 获取用户钱包验证请求参数
	 * @param reqUserPacketDTO 获取用户钱包请求参数
	 * @author YHQ 
	 * @return boolean    返回类型
	 * 
	 */
	public boolean validateTradeDetail(ResponseEntity<ResSpecialAccountTradeDetailDTO> response,ReqSpecialAccountTradeDetailDTO tradeDetailDTO){
		
		// 判断客户端请求数据是否为空
		ValiResult valiResult = ValidateUtil.validateBeanIsAvaliable(tradeDetailDTO, ReqSpecialAccountTradeDetailDTO.class);
		
		if (!valiResult.isAvailable()) {
			response.setCode(HttpStatus.SC_FORBIDDEN);
			response.setErrorMessage(DepositCodeMsg.DATA_NULL_PARAM_MESSAGE);// 客户端请求数据为空
			response.setPromptMessage(DepositCodeMsg.APP_ERROR);// 客户端请求数据为空
			response.setNullProperties(valiResult.getNullFieldName());
			log.error("为空字段为：" + valiResult.getNullFieldName());
			return false;
		}
		
		// 获取专车司机的账户信息
		Specialuseraccount specialuseraccount = specialUserAccountDao.getSpecialUserAccount(tradeDetailDTO.getUserId());
		
		// 用户账户信息不存在
		if(specialuseraccount == null){
			response.setCode(HttpStatus.SC_FORBIDDEN);
			response.setErrorMessage(DepositCodeMsg.USER_IS_NOT_EXIST);// 用户不存在
			response.setPromptMessage(DepositCodeMsg.USER_IS_NOT_EXIST);// 用户不存在
			return false;
		}
		
		return true;
	}
	
	/**
	 * 
	 * @Title: incomeAndExpensesList 
	 * @Description:  收支明细列表
	 * @param tradeDetailDTO 获取收支明细请求Bean
	 * @author YHQ 
	 * @since V2.4.0
	 * @return void    返回类型
	 * 
	 */
	public void incomeAndExpensesList(ResponseEntity<ResSpecialAccountTradeDetailDTO> response,ReqSpecialAccountTradeDetailDTO tradeDetailDTO){
		
		// 收支明细列表，客户端响应Bean
		ResSpecialAccountTradeDetailDTO resSpecialAccountTradeDetailDTO = new ResSpecialAccountTradeDetailDTO();
		
		// 根据用户Id,查询收支列表
		List<SpecialAccountTradeDetailDTO> specialAccountTradeDetailDTOs = specialAccountTradeDetailDao.getSpecialAccountTradeDetail(tradeDetailDTO.getUserId(), tradeDetailDTO.getPageIndex(), tradeDetailDTO.getPageSize());
	
		// 收支列表不为空，将交易时间转为String类型
		if(specialAccountTradeDetailDTOs != null && specialAccountTradeDetailDTOs.size() > 0){
			
			for (SpecialAccountTradeDetailDTO specialAccountTradeDetailDTO : specialAccountTradeDetailDTOs) {
				
				specialAccountTradeDetailDTO.setStandardTradeTime(DateFormatUtil.yyyy_MM_dd_HHmmss(specialAccountTradeDetailDTO.getTradeTime()));
			}
		}
		
		resSpecialAccountTradeDetailDTO.setDriverAccountList(specialAccountTradeDetailDTOs);
		
		response.setResponseBody(resSpecialAccountTradeDetailDTO);
	}
	
	/**
	 * 
	 * @Title: cumulativeDetail 
	 * @Description: 累计明细(总支出、总收入和总提现)
	 * @param userId 用户id
	 * @author YHQ 
	 * @since V2.4.0
	 * @return void    返回类型
	 * 
	 */
	public void cumulativeDetail(ResponseEntity<ResSpecialAccountTradeDetailDTO> response,String userId){
		
		// 根据用户Id专车用户账号交易明细列表
		List<Specialaccounttradedetail> specialaccounttradedetails = specialAccountTradeDetailDao.getSpecialAccountTradeDetail(userId);
		
		// 累计收入
		BigDecimal cumulativeIncome = new BigDecimal(0);
		// 累计提现
		BigDecimal cumulativeDeposit = new BigDecimal(0);
		
		if(specialaccounttradedetails != null && specialaccounttradedetails.size() > 0){
			
			for (Specialaccounttradedetail specialaccounttradedetail : specialaccounttradedetails) {
				
				if(specialaccounttradedetail.getTradeType() == 1){ // 专车订单收入
					cumulativeIncome = cumulativeIncome.add(specialaccounttradedetail.getTradeMoney());
				}else if(specialaccounttradedetail.getTradeType() == 2){ // 提现
					cumulativeDeposit = cumulativeDeposit.add(specialaccounttradedetail.getTradeMoney());
				}
				
			}
			
		}
		
		// 累计提现
		response.getResponseBody().setTotalDeposit(cumulativeDeposit);
		// 累计收入
		response.getResponseBody().setTotalIncome(cumulativeIncome);
		// 累计支出
		response.getResponseBody().setTotalExpend(new BigDecimal(0));
		
	}

}
